/*
generate_graph.cpp: Sample Biopp usage file.
    Copyright (C) 2009  Daniel Gutson, FuDePAN

    This file is part of Biopp.

    Biopp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Biopp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Biopp.  If not, see <http://www.gnu.org/licenses/>.

    NOTE: This file is in prototype stage, and is under active development.
*/

#include <iostream>
using namespace std;

#include "biopp/bio_molecular/bio_molecular.h"

using namespace biopp;

static void init_dot();
static void generate_triplets();
static void close_dot();

static string AminoName(Aminoacid aa);

/* Commented to void compilation warning
static void short_form()
{
    map<std::pair<Aminoacid, Aminoacid>, size_t> m;

    for (TripletsIterator triplet = GeneticCode::allTriplets(); !triplet.end(); ++triplet)
    {
        for (TripletsIterator triplet2 = triplet->mutate_single(G, A); !triplet2.end(); ++triplet2)
        {
            m[std::pair<Aminoacid, Aminoacid>(triplet->translate(), triplet2->translate())]++;
        }
        //cout << triplet->translate() << " " << triplet->to_string() << endl;
    }

    for (map<std::pair<Aminoacid, Aminoacid>, size_t>::const_iterator it = m.begin(); it != m.end(); ++it)
        cout << "\t" << AminoName(it->first.first) << " -> " <<  AminoName(it->first.second) << " [ label = \"" << it->second << "\"]" << endl;
}
*/

static void full_form()
{
    generate_triplets();

    for (TripletsIterator triplet = GeneticCode::allTriplets(); !triplet.end(); ++triplet)
    {
        for (TripletsIterator triplet2 = triplet->mutate_single(G, A); !triplet2.end(); ++triplet2)
            cout << "\t" << triplet->to_string() << " -> " << triplet2->to_string() << ";" << endl;
    }
}

int main()
{
    init_dot();
    full_form();
    close_dot();

    NucSequence seq("aattcggggg");
    AminoSequence as;
    seq.translate(as);
    for (size_t i = 0; i < as.size(); i++)
        cout  << AminoName(as[i]);
    cout << endl;
    return 0;
}

void init_dot()
{
    cout << "digraph G {" << endl;
}

void generate_triplets()
{
    size_t subg = 0;

    for (AminoacidsIterator amino = GeneticCode::allAminoacids(); !amino.end(); ++amino)
    {
        bool first = true;

        for (TripletsIterator t = GeneticCode::triplets(*amino); !t.end(); ++t)
        {
            if (t->contains(G) || t->contains(A))
            {
                if (first)
                {
                    cout << "\tsubgraph cluster_" << subg << "{" << endl;
                    cout << "\t\tlabel = \"" << AminoName(*amino) << "\";" << endl;
                    first = false;
                }
                cout << "\t\t" << t->to_string() << ";" << endl;
            }
        }

        if (!first)
        {
            cout << "\t}" << endl;
            subg++;
        }
    }
}

void close_dot()
{
    cout << "}" << endl;
}

static string AminoName(Aminoacid aa)
{
    if (aa == STOP_CODON)
        return "TER";
    else
    {
        char tmp[2] = { std::toupper(aa), 0 };
        return tmp;
    }
}

